﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class XemPhieuMuaHang : Form
    {
        private string _maPhieu;
        private int _quytrinhID;

        public XemPhieuMuaHang(string MaPhieu)
        {
            InitializeComponent();
            _maPhieu = MaPhieu;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            loadData();
        }

        private void XemPhieuMuaHang_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.PhieuMuaHang p = dc.PhieuMuaHangs.Single(x => x.MaPhieu == _maPhieu);
            
            txtMaPhieu.Text = p.MaPhieu;
            txtNguoiTao.Text = p.NguoiTao.Ten;
            txtPhongBan.Text = (p.NguoiTao.PhongBan == null) ? "" : p.NguoiTao.PhongBan.TenPhongBan;
            txtNgayTao.Text = p.NgayTao.ToLongTimeString();
            txtNoiDung.Text = p.NoiDung;
            txtPhieuDeNghi.Text = p.QuyTrinhMuaHangs.Single().PhieuDeNghiMuaHang.MaPhieu;

            int count = 1;
            var listHangHoa = p.QuyTrinhMuaHangs.Single().PhieuDeNghiMuaHang.PhieuDeNghiMuaHang_HangHoas;
            foreach (_DB.PhieuDeNghiMuaHang_HangHoa h in listHangHoa)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvHangHoa);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = h.HangHoa_NhaCungCap.HangHoa.TenHangHoa;
                r.Cells[2].Value = h.SoLuongMua;
                r.Cells[3].Value = h.HangHoa.DonVi;
                r.Cells[4].Value = h.HangHoa_NhaCungCap.NhaCungCap.TenNhaCungCap;
                r.Cells[5].Value = h.HangHoa_NhaCungCap.Gia;

                dgvHangHoa.Rows.Add(r);
            }

            _quytrinhID = p.QuyTrinhMuaHangs.Single().ID;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OtherForms.XemPhieuDeNghiMuaHangForm f = new OtherForms.XemPhieuDeNghiMuaHangForm(txtPhieuDeNghi.Text);
            f.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            XemDonDatHang f = new XemDonDatHang(_quytrinhID);
            f.ShowDialog();
        }

        private void btnQuyTrinh_Click(object sender, EventArgs e)
        {
            XemQuyTrinh f = new XemQuyTrinh(_quytrinhID);
            f.Show();
        }
    }
}
